#include "drv_timer.h"
#include "custom.h"

static uint32_t sd_timer_cnt = 0xFFFFFFFF, hb_timer_cnt = 0;

//https://blog.csdn.net/weixin_45682654/article/details/128571696
static int file_data_read(char *filename, char *str)
{
    int ret = 0;
    FILE *data_stream;

    data_stream = fopen(filename, "r"); /* 只读打开 */
    if(data_stream == NULL) {
        printf("can't open file %s\r\n", filename);
        return -1;
    }

    ret = fscanf(data_stream, "%s", str);
    if(!ret) {
        printf("file read error!\r\n");
    } else if(ret == EOF) {
        /* 读到文件末尾的话将文件指针重新调整到文件头 */
        fseek(data_stream, 0, SEEK_SET);  
    }
    fclose(data_stream);    /* 关闭文件 */    
    return 0;
}

uint32_t vdata_get_hb_timer_cnt(void)
{
    return hb_timer_cnt;
}

void vdata_set_hb_timer_cnt(uint32_t cnt)
{
    hb_timer_cnt = cnt;
}

uint32_t vdata_get_sd_timer_cnt(void)
{
    return sd_timer_cnt;
}

void vdata_set_sd_timer_cnt(uint32_t cnt)
{
    sd_timer_cnt = cnt;
}

uint8_t fun_ScheduleRunWeek(uint8_t *data)
{
    uint8_t tmp_Return;
    uint8_t i;

    if (data[1] == 0){
        return (0);
    }

    // tmp_Return = 0;
    // for (i = 0; i < 7; i++){
        // if (i == g_CurrentRTC.Week){
            // if (data[0] & (1 << i)){
                tmp_Return = 2;
                if ((g_CurrentRTC.Hour == data[4]) && (g_CurrentRTC.Minute == data[5]) && (g_CurrentRTC.Second <= 10)){
                    tmp_Return = 1;
                    g_UserSet.Updata2CommCnt = 8;
                    g_UserSet.f_On = data[2];
                    // g_UserSet.Mode = data[2];
                    // g_UserSet.SetTemp[g_UserSet.Mode] = data[3] * 10;
                }
            // }
        // }
    // }

    // guiInfo("tmp_Return -> %d\r\n", tmp_Return);
    return (tmp_Return);
}

void vdata_timer_proc(void* arg)
{
    char end, temp_buff[100] = {0};
    int s_temp = 0, s_temp2 = 0, i = 0, j = 0;
    unsigned int conf_hour, conf_min , conf_sec;
    static int blk_time_count = 0, timer_1min = 0;

    switch ( mcu_get_wifi_work_state())
    {
        case 0x00: g_wifi_status = "SMART_CONFIG_STATE";break;
        case 0x01: g_wifi_status = "AP_STATE";break;
        case 0x02: g_wifi_status = "WIFI_NOT_CONNECTED";break;
        case 0x03: g_wifi_status = "WIFI_CONNECTED";break;
        case 0x04: g_wifi_status = "WIFI_CONN_CLOUD";break;
        case 0x05: g_wifi_status = "WIFI_LOW_POWER";break;
        case 0x06: g_wifi_status = "SMART_AND_AP_STATE";break;
        default: g_wifi_status = "WIFI_SATE_UNKNOW";break;
    }

    if(g_UserSet.BlkMode == 0){
        g_UserSet.blk_time_count = 60;
    }
    if(g_UserSet.blk_time_count){
        g_UserSet.blk_time_count--;
    }
    if(!blk_time_count && g_UserSet.blk_time_count){
        fun_set_light_level((float)(g_UserSet.blk_level + 10)/100.0f);
    }
    if(blk_time_count && !g_UserSet.blk_time_count){
        fun_set_light_level(0);
    }
    blk_time_count = g_UserSet.blk_time_count;

    if(g_Timer8DispCommErrCnt < 20){
        g_Timer8DispCommErrCnt++;
    }else{
        g_UserSet.ErrorCode = 'E' * 256 + 32;
        g_sys_error_count = 1;
    }
    if(g_UserSet.UpgradeTimer){
        g_UserSet.UpgradeTimer--;
    }
    if(g_UserSet.UpgradeTimeout){
        g_UserSet.UpgradeTimeout--;
    }

    // for( i = 0; i < 8; i++ ){
    //     s_temp += fun_ScheduleRunWeek( &(g_MemSetSchedule[i*6]) );
    // }
    g_UserSet.ScheNum = s_temp;

    file_data_read("/sys/class/hwmon/hwmon1/temp1_input", temp_buff);
    g_UserSet.Test_TEMP = atof(temp_buff)/1000.0f;
    // printf("temp1_input -> %f...\r\n", g_UserSet.Test_TEMP);

    file_data_read("/sys/class/hwmon/hwmon1/humidity1_input", temp_buff);
    g_UserSet.Test_HUM = atof(temp_buff)/1000.0f;
    // printf("humidity1_input -> %f...\r\n", g_UserSet.Test_HUM);

    g_UserSet.TimerPwrOn++;
}

void *thread_sec_timer(void *argc)
{
    static int timer_1s, timer_100ms;
    int len = 0, wifi_status = 0;
    while(1) {
        if(timer_100ms < 100){
            timer_100ms++;
        } else {
            timer_100ms = 0;
            if((mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)&&(wifi_status != WIFI_CONN_CLOUD)){
                mcu_get_system_time();
            }else if(mcu_get_wifi_work_state() == WIFI_CONN_CLOUD){
                all_data_update();
            }
            wifi_status = mcu_get_wifi_work_state();
        }
        if(timer_1s < 1000){
            timer_1s++;
        } else {
            timer_1s = 0;
            vdata_timer_proc(0);
        }
        sleep_ms(1);
    }
}

void timer_start(void)
{
    int res = 0;
    pthread_t tid_sec_timer;

    res = pthread_create(&tid_sec_timer , NULL , thread_sec_timer , NULL);
    if(res){
        perror("thread_sec_timer creat error!\r\n");
    }
}
